package com.telnetnz.ticketdesk.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import com.telnetnz.ticketdesk.bean.Attachment;
import com.telnetnz.ticketdesk.common.config.SpringContext;
import com.telnetnz.ticketdesk.dao.entity.AttachmentEntity;
import com.telnetnz.ticketdesk.dao.wrapper.AttachmentWrap;

public class AttachmentService
{
	public static final String[] ORDER_BY = AttachmentWrap.ORDER_BY;
	public static final int ORDERBY_CREATE_TIME = 0;

	public static final String[] ORDER = AttachmentWrap.ORDER;

	public static final String[] TYPES = {"", "picture", "video", "other"};
	public static final int TYPE_PICTURE = 1;
	public static final int TYPE_VIDEO = 2;
	public static final int TYPE_OTHER = 3;

	public static final long SIZE_LIMIT = 10*1024*1024L;

	private static Logger logger = Logger.getLogger(AttachmentService.class);

	public static int getFileType(String mimeType)
	{
		if (mimeType == null || mimeType.length() == 0)
			return TYPE_OTHER;
		else if (mimeType.indexOf("image") == 0)
			return TYPE_PICTURE;
		else if (mimeType.indexOf("video") == 0)
			return TYPE_VIDEO;
		else
			return TYPE_OTHER;
	}

	/*
	 * Basic methods
	 */
	public static long countAll()
	{
		return getWrap().countAll();
	}

	public static Attachment getAttachment(long id)
	{
		AttachmentEntity entity = getWrap().select(id);
		return Attachment.adapt(entity);
	}

	public static void add(Attachment file)
	{
		if (file == null)
			return;
		getWrap().insert(file.toEntity());
	}

	/**
	 * This will check if the file path has been updated, if yes, the old physical file will be removed
	 * The file items derived from this file need to be deleted by invoking AttachmentItemService.deleteFileItemsByFileId() 
	 * 
	 * @param file
	 */
	public static void update(Attachment file)
	{
		if (file == null)
			return;

		// If the physical file has been changed, delete the old file
		Attachment original = getAttachment(file.getId());
		if (!original.getPath().equals(file.getPath()))
		{
			File tmpFile = new File(original.getFullPath());
			if (tmpFile.isFile()) // VERY IMPORTANT: To avoid *whole folder deletion*
				FileUtils.deleteQuietly(tmpFile);
		}

		getWrap().update(file.toEntity());
	}

	public static void deleteByPrimaryKey(long id)
	{
		Attachment file = getAttachment(id);
		if (file != null)
			delete(file);
	}

	/**
	 * Delete one file along with its physical file and derived file items
	 * 
	 * @param file
	 */
	public static void delete(Attachment file)
	{
		if (file == null)
			return;

		// Delete itself
		File tmpFile = new File(file.getFullPath());
		if (tmpFile.isFile()) // VERY IMPORTANT: To avoid *whole folder deletion*
			FileUtils.deleteQuietly(tmpFile);

		getWrap().delete(file.toEntity());
	}

	public static List<Attachment> getAttachmentsByPageAndCondition(
			int start,
			int limit,
			int orderby,
			int order,
			long ticketId,
			long commentId,
			long creatorId)
	{
		List<Long> ids = getWrap().selectByPageAndCondition(
				start, limit, orderby, order, ticketId, commentId, creatorId);
		return adaptIdsList(ids);
	}

	public static long countByCondition(
			long ticketId,
			long commentId,
			long creatorId)
	{
		return getWrap().countByCondition(ticketId, commentId, creatorId);
	}

	private static List<Attachment> adaptIdsList(List<Long> ids)
	{
		List<Attachment> attachments = new ArrayList<Attachment>();
		if (ids != null && ids.size() > 0)
		{
			for (long id : ids)
			{
				Attachment attachment = getAttachment(id);
				if (attachment != null)
					attachments.add(attachment);
			}
		}
		return attachments;
	}

	private static AttachmentWrap getWrap()
	{
		return SpringContext.getAttachmentWrap();
	}

	
}
